import pygame
from pygame.locals import *
from shape import *
import house
import display
from threading import *

class pygame_loop(Thread):
    def __init__(self):
        global window
        Thread.__init__(self)
        pygame.init()
        window = pygame.display.set_mode((640, 480))
        display.surface = window
        pygame.display.set_caption('tangram solver')
        self.quit = False
    def run(self):
        while self.quit == False:
            main_loop()
            pygame.display.flip()
            pygame.event.pump()
            for event in pygame.event.get():                
                if event.type == pygame.QUIT:
                    pygame.quit()
                    self.quit = True
                elif event.type == MOUSEBUTTONDOWN:
                    print "Mouse Position: " + str(display.display_coords_to_object_coords(event.pos))
display1 = []
display_shape_lock = Lock()
def main_loop():
    pygame.draw.rect(window, (255,255,255),pygame.Rect(0, 0, 640, 480))
    display_shape_lock.acquire()
    for shape in display1:
        display.display_shape(shape)
    for shape in display2:
        shape.translate((0, 30))
        display.display_shape(shape)
        shape.translate((0, -30))
    display_shape_lock.release()
square = shape('shapes/square.txt','square')
large_tri1 = shape('shapes/large_triangle.txt','tri1')
large_tri2 = shape('shapes/large_triangle.txt','tr2')
tri3 = shape('shapes/triangle.txt', 'tri3')
small_tri1 = shape('shapes/small_triangle.txt','')
small_tri2 = shape('shapes/small_triangle.txt', '')
parallelogram = shape('shapes/parallelogram.txt')

#shapes=
shape1=shape('shape1.txt','shape1')
#shape1.scale(2, 2)
P = [large_tri1, large_tri2, square]
display1 = P+[house.house]
display2 = []
display_loop = pygame_loop()
display_loop.start()
tmp = None
try:
    print fitshapes2(P, house.house)
except ValueError, val:
    tmp = val
#s1 = house.house.subtract_shape(large_tri1)
#s1 = s1.subtract_shape(large_tri2)
#logging.on('match_vertex_pair', True)
#point1 = square.vertices[1][0]
#point2 = s1.vertices[3][0]
#square.translate((point2[0] - point1[0], point2[1] - point1[1]))        
#g = match_vertex_pair(square.vertices[1], s1.vertices[3], square, s1)
#print g.next()
#logging.on('match_vertex_pair', False)

#try:
#print fitshapes([large_tri1, large_tri2, square, parallelogram, tri3, small_tri1, small_tri2], house.house)
#    print fitshapes([large_tri1, large_tri2], house.house)
#except ValueError, val:
#    print 'segment2 = ' + str(val)
#    tmp = val
#fitshapes([square, large_tri1, large_tri2, tri3, small_tri1, small_tri2, parallelogram], house.house)
#fitshapes([tri1, square], shape1)
#display1 = [large_tri1, large_tri2, tri3, small_tri1, small_tri2, square, parallelogram]
#fitshape(square, shape1, 0, 0)
#s1 = shape1.subtract_shape(square)

#print fitshapes([tri1, square], shape1)
#print fitshape(triangle1, shape1, 1)
#logging.on('subtract_shape', False)
#print fitshapes([triangle1, triangle2], shape1)
#logging.on('subtract_shape', True)
#fitshapes([triangle1, triangle2, square], shape1)
#s1 = shape1.subtract_shape(triangle1)
#logging.on('subtract_shape', False)

#fitshapes()
#print "square._offset:"+str(square._offset)
#print "triangle1._offset:"+str(triangle1._offset)
#print "triangle2._offset:"+str(triangle2._offset)
#def init():
#s = shape1.subtract_shape(square)
#fitshape(triangle1, s, 1)
#s1 = s.subtract_shape(triangle1)
#logging.on('fitshape', True)
#fitshape(triangle2, s1, 1)
#logging.on('fitshape', False)
#display_shape = triangle2
    



